package com.neuedu.oasystem.view;

import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Privilege;
import com.neuedu.oasystem.service.PrivilegeService;
import com.neuedu.oasystem.service.impl.PrivilegeServiceImpl;

public class PrivilegeSystem {
	private Scanner scanner = null;
	Privilege privilege = null;
	private PrivilegeService privilegeService;

	public void init() {
		System.out.println("========================================");
		System.out.println("           欢迎使用权限管理系统                                  ");
		System.out.println("========================================");
		System.out.println("1.权限管理");
		System.out.println("2.返回上一级");
		System.out.println("========================================");
	}

	public void work() {
		scanner = new Scanner(System.in);
		privilegeService = new PrivilegeServiceImpl();
		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1":
				// 调用用管理模块
				System.out.println("调用权限管理模块");
				userManager();
				break;
			case "2":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}
	}

	private void userManager() {

		boolean running = true;
		while (running) {
			System.out.println("========================================");
			System.out.println("            您选了权限管理模块                                   ");
			System.out.println("========================================");
			System.out.println("1.新增权限");
			System.out.println("2.修改权限");
			System.out.println("3.权限列表");
			System.out.println("4.删除权限");
			System.out.println("5.返回上一级");
			System.out.println("========================================");
			System.out.println("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("新增权限 ");
				privilegeAdd();
				break;
			case "2":
				System.out.println("修改权限 ");
				privilegeEdit();				
				break;
			case "3":
				System.out.println("权限列表");
				privilegeList();
				break;
			case "4":
				System.out.println("删除权限 ");
				privilegeDelete();
				break;
			case "5":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}

		}

	}
	private void privilegeAdd() {
		privilegeService = new PrivilegeServiceImpl();
		System.out.println("请输入权限ID:           ");
		String privilegeIdString;
		do {
			try {

				privilegeIdString = scanner.next();
				if (Integer.valueOf(privilegeIdString) instanceof Integer) {
					break;
				} else {
					privilegeIdString = scanner.next();
				}
			} catch (Exception e) {
				System.out.println("您的输入有误，请重新输入：");
			}
		} while (true);
		int privilegeId = Integer.parseInt(privilegeIdString);

		System.out.println("请添加权限名称：");
		String privilegeName = scanner.next();
		System.out.println("请添加权限描述：");
		String privilegeDesc = scanner.next();
		privilege = new Privilege(privilegeId, privilegeName, privilegeDesc);
		
		boolean b = privilegeService.add(privilege);
		if (b) {
			System.out.println("添加成功！");
		}else {
			System.out.println("添加失败！");
		}
	}

	private void privilegeEdit() {
		privilegeList();
		privilegeService = new PrivilegeServiceImpl();
		System.out.println("请输入修改权限的Id：");
		int privilegeId = scanner.nextInt();
		System.out.println("请输入新的权限名称:       ");
		String privilegeName = scanner.next();
		System.out.println("请输入新的权限描述:        ");
		String privilegeDesc = scanner.next();
		privilege = new Privilege(privilegeId, privilegeName, privilegeDesc);
		boolean b = privilegeService.edit(privilege);
		if (b) {
			System.out.println("修改成功！");
		}else {
			System.out.println("修改失败");
		}
	}

	private void privilegeList() {
		privilegeService = new PrivilegeServiceImpl();
		List<Privilege> list = privilegeService.findAll();
		System.out.println("权限编号\t权限名称\t权限描述");
		for (Privilege privilege : list) {
			System.out.println(privilege.getPrivilegeId() + "\t" + privilege.getPrivilegeName() + "\t"
					+ privilege.getPrivilegeDesc());
		}
	}

	private void privilegeDelete() {
		privilegeService = new PrivilegeServiceImpl();
		privilegeList();
		System.out.println("请输入您要删除的权限编号：");
		int privilegeId = scanner.nextInt();
		try {
			privilegeService.deleteById(privilegeId);
		} catch (Exception e) {
			System.out.println("请输入正确的权限编号：");
		}	
		System.out.println("删除成功！");
		privilegeList();
	}
}
